import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-09-22
 * Time: 16:13
 */
public class ArrayListDemo1 {
    public static void main(String[] args) {

    }
    public static void main7(String[] args) {
        ArrayList<User> list = new ArrayList<>();

        //创建三个用户
        User user1 = new User("123445","zhangsan","123456");
        User user2 = new User("154892","lisi","1314");
        User user3 = new User("7411","wangwu","520");

        //把用户添加到集合中
        list.add(user1);
        list.add(user2);
        list.add(user3);

        //测试
        boolean ret = contains(list,"7411");
        System.out.println(ret);

    }
    public static Boolean contains(ArrayList<User> list,String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return true;
            }
        }
        return false;
    }
    public static void main6(String[] args) {
        //1.创建集合
        ArrayList<Student> list = new ArrayList<>();
        //创建的集合初始长度是0

        //2.键盘录入学生信息并添加到集合当中
        Scanner scanner = new Scanner(System.in);
        //录入3个学生信息
        for (int i = 0; i < 3; i++) {
            Student student = new Student();

            System.out.println("请输入学生姓名");
            String name = scanner.next();
            System.out.println("请输入学生年龄");
            int age = scanner.nextInt();

            //把name和age赋值给学生对象
            student.setName(name);
            student.setAge(age);

            //把学生对象添加到集合中
            list.add(student);
        }
        //3.遍历
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName() + "," + stu.getAge());
        }
    }
    public static void main5(String[] args) {
        //1.创建集合
        ArrayList<Student> list = new ArrayList<>();
        //2.创建学生对象
        Student s1 = new Student("zhangsan",15);
        Student s2 = new Student("lisi",19);
        Student s3 = new Student("wangwu",23);

        //3.添加元素
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //4.遍历元素
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName() + "," + stu.getAge());
        }

    }
    public static void main4(String[] args) {
        //1.创建集合
        ArrayList<Integer> list = new ArrayList<>();
        //2.添加元素
        list.add(123);
        list.add(456);
        list.add(1314);
        //3.遍历元素
        System.out.print("[");
        for (int i = 0; i < list.size(); i++) {
            int tmp = list.get(i);
            if(i == list.size() - 1){
                System.out.print(tmp + "]");
            }else {
                System.out.print(tmp + ",");
            }
        }
    }
    public static void main3(String[] args) {
        //1.创建集合
        ArrayList<String> list = new ArrayList<>();
        //2.添加元素
        list.add("aaa");
        list.add("bbb");
        list.add("123");
        //3.遍历元素
        System.out.print("[");
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            if(i == list.size() - 1){
                System.out.print(str + "]");
            }else {
                System.out.print(str + ",");
            }

        }
    }
    public static void main2(String[] args) {
        //1.创建集合
        ArrayList<String> list = new ArrayList<>();

        //2.添加元素
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        System.out.println(list);

        //3.删除元素
        boolean ret = list.remove("aaa");
        System.out.println(ret);
        //删除失败
        boolean ret1 = list.remove("dcd");
        System.out.println(ret1);

        //根据索引删除
        String str = list.remove(0);
        System.out.println(str);//此时已经删除了字符串"bbb"
        System.out.println(list);


        //4.修改元素
        String str1 = list.set(0,"ddd");
        System.out.println(str1);
        System.out.println(list);

        //5.查询元素
        String str2 = list.get(0);//如果对应下标位置没有元素，则报错
        System.out.println(str2);

        //6.集合长度
        int length = list.size();
        System.out.println(length);

        //7.遍历集合
        for (int i = 0; i < list.size(); i++) {
            String str3 = list.get(i);
            System.out.println(str3);
        }

    }
    public static void main1(String[] args) {
        //方式1，jdk7之前定义方法
        ArrayList<String> list = new ArrayList<String>();

        //方式2，jdk7之后定义方法
        ArrayList<String> list1 = new ArrayList<>();

        System.out.println(list1);

        int[] arr = new int[2];
        System.out.println(arr);
    }
}
